/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.dashboard.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.tianyun.cloud.common.dto.RestResult;
import org.tianyun.cloud.dashboard.client.SampleFeignClient;
import org.tianyun.cloud.dashboard.rabbit.RabbitProducerSample;
import org.tianyun.cloud.utils.JsonUtils;
import org.tianyun.cloud.web.controller.BaseController;

/**
 * SampleController
 *
 * @auther ebert_chan
 */
@RestController
@RequestMapping("/")
public class SampleController extends BaseController {

    @Value("${config.test:}")
    private String config;

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

    @Autowired
    private RabbitProducerSample rabbitProducerSample;

    @Autowired
    private SampleFeignClient sampleFeignClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/message/send")
    public RestResult<?> sendMessage() {
        rabbitProducerSample.send("Hello, World!");
        return success("Hello, World! " + this.getMessageSource().getProperty("order.pay", 1.23));
    }

    @RequestMapping("/feign/test")
    public RestResult<?> feignTest() {

        LOGGER.info("config test: {}", config);

        RestResult<?> rest = restTemplate.getForObject("http://tianyun-cloud-sample/message/send", RestResult.class);
        LOGGER.info("rest call result: {}", JsonUtils.toJsonString(rest));

        return sampleFeignClient.sendMessage();
    }

}
